//
//  TomamaUI.m
//  Tomama
//
//  Created by qianfeng on 15/12/16.
//  Copyright (c) 2015年 ZhangLuJie. All rights reserved.
//

#import "TomamaUI.h"

@implementation TomamaUI

+(CGFloat)getStringWeithtWith:(NSString *)str andHeigth:(CGFloat )h andFont:(UIFont *)font
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, h) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
    return rect.size.width;
}

+(CGFloat)getStringHeightWith:(NSString *)str andWeith:(CGFloat )w andFont:(UIFont *)font
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(w, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
    return rect.size.height;
}

+(NSString*)stringToDataString:(NSString *)str andString:(NSString *)gehsi
{
    NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:[str doubleValue]/1000];
    //实例化一个NSDateFormatter对象
    NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:gehsi];
    NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
    return currentDateStr;
}

+(UILabel *)createLabel:(CGRect)cg textAligment:(NSInteger)aligment font:(UIFont *)font textColor:(UIColor *)textColor text:(NSString *)text
{
    UILabel *label = [[UILabel alloc]initWithFrame:cg];
    label.textAlignment = aligment;
    label.font = font;
    label.textColor = textColor;
    label.text = text;
    return label;
}

+(UIImageView *)createImageView:(CGRect)cg backGroundImageV:(NSString *)imageName
{
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:cg];
    imageV.image = [UIImage imageNamed:imageName];
    return imageV;
}

+(UIButton *)createButton:(NSInteger)buttonType Frame:(CGRect)cg targ:(id)targe sel:(SEL)sel titleColor:(UIColor *)titleColor font:(UIFont*)font title:(NSString *)title  image:(NSString *)imageName backGroundImage:(NSString*)backImageName
{
    UIButton *button = [UIButton buttonWithType:buttonType];
    button.frame = cg;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor  forState:UIControlStateNormal];
    [button setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button setBackgroundImage:[[UIImage imageNamed:backImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    button.titleLabel.font = font;
    [button addTarget:targe action:sel forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+(UIViewController *)getTopVC
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    if (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    
    if ([topVC isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarVC = (UITabBarController *)topVC;
        UINavigationController *navi = tabBarVC.viewControllers[tabBarVC.selectedIndex];
        
        return [navi.viewControllers lastObject];
    }
    else if ([topVC isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navi = (UINavigationController *)topVC;
        return [navi.viewControllers lastObject];
    }
    else{
        return topVC;
    }
}


@end
